<template>
  <div id="admin_id">
    <!-- 上层注释以及添加按钮 -->
    <div class="admin_id_top">
      <div class="admin_id_left">
        注：<br />
        1.增加的新栏目为当前栏目的下一行<br />
        2.修改完的栏目一行需要后提交审批才能生效
      </div>
      <div class="admin_id_right">
        <el-button round @click="xianshi">增加</el-button>
      </div>
    </div>
    <!-- 下层渲染表格  -->
    <el-table
      :data="admin_list"
      style="width: 100%; margin-bottom: 20px; margin-top: 20px"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="username" label="用户名"> </el-table-column>
      <el-table-column prop="rolename" label="所属角色"> </el-table-column>
      <el-table-column label="状态" prop="status" v-slot="$s" width="155">
        <template>
          <el-button round size="mini" type="success" v-if="$s.row.status == 1">
            已启用
          </el-button>
        </template>
        <template>
          <el-button round size="mini" type="danger" v-if="$s.row.status == 0">
            已禁用
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" v-slot="caozuo">
        <template>
          <el-button
            round
            class="xiugai"
            type="warning"
            size="mini"
            @click="xiugai(caozuo.row)"
            >修改</el-button
          >
        </template>
        <template>
          <el-button
            round
            class="shanchu"
            type="danger"
            size="mini"
            @click="shanchu(caozuo.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="zongshu"
      style="text-align: center"
      @current-change="change_page"
      :page-size="page_size"
    >
    </el-pagination>

    <addadmin
      :isShow="isShow"
      @emitaddadmin="emitaddadmin"
      ref="addadmin_ref"
    ></addadmin>
  </div>
</template>

<script>
import addadmin from "./addadmin.vue";
import { mapActions, mapState } from "vuex";
export default {
  data() {
    return {
      // 当前页数
      page: 1,
      page_size: 5,
      total: 0,
      isShow: { sta: false },
    };
  },
  created() {
    this.AdminActions({ page: this.page, size: this.page_size });
    this.AdminZongshu();
  },
  computed: {
    ...mapState({
      admin_list: (state) => state.AdminStore.admin_list,
      zongshu: (state) => state.AdminStore.zongshu,
    }),
  },
  methods: {
    shanchu(id) {
      this.$del_button(() => {
        this.$http.post("/api/userdelete", { id }).then((res) => {
          if (res.code == 200) {
            alert("删除成功");
            this.AdminZongshu();
            this.AdminActions({ page: this.page, size: this.page_size });
          }
        });
      });
    },
    xiugai(obj) {
      this.isShow.sta = true;
      this.$refs.addadmin_ref.addxiugai(obj);
    },
    xianshi() {
      this.isShow.sta = true;
    },
    change_page(page) {
      this.page = page;
      this.AdminActions({ page: this.page, size: this.page_size });
    },
    emitaddadmin() {
      this.AdminZongshu();
      this.AdminActions({ page: this.page, size: this.page_size });
    },
    ...mapActions("AdminStore", ["AdminActions", "AdminZongshu"]),
  },
  components: {
    addadmin,
  },
};
</script>

<style>
#admin_id .admin_id_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#admin_id .admin_id_top .admin_id_left {
  font-size: 12px;
  color: #c4c4c4;
}
#admin_id .admin_id_top .admin_id_right button {
  height: 10px;
  line-height: 2px;
  font-size: 12px;
  color: #ffa998;
}
</style>